package edu.whu.puppygo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import edu.whu.puppygo.assistant.HealthAssistant;
import edu.whu.puppygo.base.HttpResponse;
import edu.whu.puppygo.dao.ConversationDao;
import edu.whu.puppygo.dao.FragmentDao;
import edu.whu.puppygo.dao.PetDao;
import edu.whu.puppygo.dao.UserDao;
import edu.whu.puppygo.dto.EmbeddingResultDto;
import edu.whu.puppygo.entity.*;
import edu.whu.puppygo.service.EmbeddingService;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @Author xinjiezhuang
 * @Date 2023-11-30
 * @Description
 **/
@RestController
@Slf4j
@RequestMapping("/embedding")

public class EmbeddingController {
    @Resource
    private EmbeddingService embeddingService;

    @Resource
    private HealthAssistant healthAssistant;

    @Resource
    private FragmentDao fragmentDao;

    @Resource
    private ConversationDao conversationDao;

    @Resource
    private PetDao petDao;

    @Resource
    private UserDao userDao;

    @ApiModelProperty(value = "发送消息,用于测试")
    @PostMapping(value = "/send")
    public HttpResponse<String> sendMsg(String text, Long fragmentId) {
//        embeddingService.sendMsg(text, fragmentId);

        Fragment new_fragment = fragmentDao.findById(fragmentId).get();
        log.info(new_fragment.toString());
        Conversation new_conversation = conversationDao.findById(new_fragment.getConversationId()).get();
        log.info(new_conversation.toString());
        User user = userDao.findById(new_conversation.getUserId()).get();
        log.info(user.getId().toString());
        Long mainPetId = new_conversation.getPetId();
        Pet pet = petDao.findById(mainPetId).orElseThrow(() -> new RuntimeException("宠物不存在"));
//        String answer = resultBody.getAnswer();
        String answer = " ";
        Fragment fragment = fragmentDao.findById(fragmentId).orElseThrow(() -> new RuntimeException("问答不存在"));
        log.info(fragment.toString());
        fragment.setKnowledgeResult(answer);
        log.info("setKnowledgeResult下面");
        fragment.setStatue(FragmentStatueEnum.EMBEDDING_FINISHED);
        fragmentDao.save(fragment);
        log.info(new_conversation.getId().toString());
        log.info(pet.toString());
        log.info(fragment.getQuestion().toString());
        log.info(fragment.getImgResult().toString());
        log.info(fragment.getKnowledgeResult());

        String diagnose = healthAssistant.getDiagnose(new_conversation.getId(), pet.toString(), fragment.getQuestion(), fragment.getImgResult(), fragment.getKnowledgeResult());
        fragment.setAnswer(diagnose);
        fragment.setStatue(FragmentStatueEnum.GPT_FINISHED);
        fragmentDao.save(fragment);
        return HttpResponse.success("success");

    }

    @PostMapping("/callback")
    @Transactional
    public HttpResponse<String> handleCallback(@Valid @RequestBody EmbeddingResultDto resultBody) throws JsonProcessingException {
        log.info("进入callback了");
        Fragment new_fragment = fragmentDao.findById(resultBody.getFragmentId()).get();
        log.info(new_fragment.toString());
        Conversation new_conversation = conversationDao.findById(new_fragment.getConversationId()).get();
        log.info(new_conversation.toString());
        User user = userDao.findById(new_conversation.getUserId()).get();
        log.info(user.getId().toString());
        Long mainPetId = new_conversation.getPetId();
        Pet pet = petDao.findById(mainPetId).orElseThrow(() -> new RuntimeException("宠物不存在"));
//        String answer = resultBody.getAnswer();
        String answer = " ";
        Fragment fragment = fragmentDao.findById(resultBody.getFragmentId()).orElseThrow(() -> new RuntimeException("问答不存在"));
        log.info(fragment.toString());
        fragment.setKnowledgeResult(answer);

        fragment.setStatue(FragmentStatueEnum.EMBEDDING_FINISHED);
        fragmentDao.save(fragment);
        log.info(new_conversation.getId().toString());
        log.info(pet.toString());
        log.info(fragment.getQuestion().toString());
//        log.info(fragment.getImgResult().toString());
        log.info(fragment.getKnowledgeResult());
        String diagnose;
        if(fragmentDao.countByConversationId(new_conversation.getId())>1){
            diagnose = healthAssistant.chat(new_conversation.getId(),fragment.getQuestion());
        }else{
            diagnose = healthAssistant.getDiagnose(new_conversation.getId(), pet.toString(), fragment.getQuestion(), fragment.getImgResult(), fragment.getKnowledgeResult());
        }
        fragment.setAnswer(diagnose);
        fragment.setStatue(FragmentStatueEnum.GPT_FINISHED);
        fragmentDao.save(fragment);
        return HttpResponse.success("success");
    }
}
